﻿/*
 * Catalyst - http://catalystobjects.com
 * 
 * New BSD License (BSD)
 * 
 * Copyright (c) 2009, Ashley Brener
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of Midlet Earth nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SubSonic;

namespace Catalyst
{
	public class MappingList : Dictionary<string, Mapping>
	{
		public MappingList()
		{
			names = new List<string>();
		}

		public MappingList(IEnumerable<Mapping> Mappings) : this()
		{
			foreach (Mapping mapping in Mappings)
			{
				Add(mapping.Name, mapping);
			}
		}

		private List<string> names;
		public List<string> Names
		{
			get
			{
				if (Count > 0)
				{
					foreach (KeyValuePair<string, Mapping> item in this)
					{
						names.Add(item.Key);

						IRelationLoader loader = item.Value.Relation as IRelationLoader;

						if (loader != null)
						{
							names.AddRange(loader.GetMappings().Names);
						}
					}

					names.Sort();
				}
				else
				{
					names = new List<string>();
				}

				return names;
			}
		}

		///// <summary>
		///// Adds a mapping to load a Relation from the database
		///// </summary>
		///// <param name="MappingName"></param>
		///// <param name="DataTableName"></param>
		///// <param name="Relation"></param>
		//public void Add(string MappingName, string DataTableName, RelationalObject Relation)
		//{
		//    Add(MappingName, DataTableName, null, Relation);
		//}

		///// <summary>
		///// Adds a mapping to load a Relation from the database with constraints
		///// </summary>
		///// <param name="MappingName"></param>
		///// <param name="DataTableName"></param>
		///// <param name="Constraints"></param>
		///// <param name="Relation"></param>
		//public void Add(string MappingName, string DataTableName, List<Where> Constraints, RelationalObject Relation)
		//{
		//    Mapping mapping = new Mapping();
		//    mapping.DataTableName = DataTableName;
		//    mapping.Constraints = Constraints;
		//    mapping.Relation = Relation;

		//    Add(MappingName, mapping);
		//}

		//public void Add<ItemType>(string MappingName, string DataTableName, ItemType Relation)
		//    where ItemType : RelationalObject, new()
		//{
		//    Add(MappingName, DataTableName, Relation);
		//}

		public void AddRelation<T>(string MappingName, TableSchema.Table Table)
			where T : RelationalObject, new()
		{
			T relation = new T();
			add<T>(MappingName, relation, true, Table, null, null, true);
		}

		public void AddRelation<T>(string MappingName, TableSchema.Table Table, bool IsSerializable)
			where T : RelationalObject, new()
		{
			T relation = new T();
			add<T>(MappingName, relation, true, Table, null, null, IsSerializable);
		}

		//public void AddDataRelation<T>(string MappingName, string ForeignKeyName, object PrimaryKeyValue)
		//    where T : RelationalObject, new()
		//{
		//    T relation = new T();
		//    add<T>(MappingName, relation, true, null, null, null, true);
		//}


		//public void AddDataRelation<T>(string MappingName, string TableName, string ForeignKeyName, object PrimaryKeyValue, List<Where> Constraints, bool IsSerializable)
		//    where T : RelationalObject, new()
		//{
		//    T relation = new T();
		//    add<T>(MappingName, relation, true, TableName, null, Constraints, IsSerializable);
		//}

		public void Add<T>(string MappingName, TableSchema.TableColumn Column)
		{
			add<T>(MappingName, null, false, null, Column.ColumnName, null, true);
		}

		public void Add<T>(string MappingName, TableSchema.TableColumn Column, bool isSerializable)
		{
			add<T>(MappingName, null, false, null, Column.ColumnName, null, isSerializable);
		}

		public void Add<T>(string MappingName, string ColumnName)
		{
			add<T>(MappingName, null, false, null, ColumnName, null, true);
		}

		public void Add<T>(string MappingName, string ColumnName, bool isSerializable)
		{
			add<T>(MappingName, null, false, null, ColumnName, null, isSerializable);
		}

		private void add<T>(string mappingName, object obj, bool isDataRelation, TableSchema.Table table, string columnName, object constraints, bool isSerializable)
		{
			Mapping mapping = new Mapping();
			mapping.Name = mappingName;
			mapping.Table = table;
			mapping.ColumnName = columnName;
			mapping.Constraints = constraints;
			mapping.IsRelation = isDataRelation;
			mapping.IsSerializable = isSerializable;
			mapping.Type = typeof(T);

			mapping.Relation = obj;

			Add(mappingName, mapping);
		}
	}
}
